package moonstruckdrops.serviceImpl;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import moonstruckdrops.dto.ComentDto;
import moonstruckdrops.page.EntryPage;
import moonstruckdrops.service.BlogComentService;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.google.appengine.api.datastore.Text;

public class BlogComentServiceImpl implements BlogComentService,Serializable {

	private static final long serialVersionUID = 1L;
	
	public ListView<ComentDto> EntryComent(List<ComentDto> ComentList){
		ListView<ComentDto> list=new ListView<ComentDto>("list",ComentList){
			private static final long serialVersionUID = 1L;
			protected void populateItem(ListItem<ComentDto> item){
				ComentDto ComentEntity=item.getModelObject();
				String ComentDate="";
				if(ComentEntity.getDate()!=null){
					ComentDate =new SimpleDateFormat("yyyy-MM-dd").format(ComentEntity.getDate().getTime());
				}
				item.add(new Label("ComentDate",new Model<String>(ComentDate)));
				item.add(new Label("ComentUser",new Model<String>(ComentEntity.getUserName())));
				item.add(new Label("Coment",new Model<String>(ComentEntity.getComent().getValue())));
			}
		};
		return list;
	}
	
	public Form<Void> ComentForm(final Long EntryID){
		final TextField<String> UserName =new TextField<String>("UserName",new PropertyModel<String>(coment,"inputUserNameText"));
		final TextArea<String> EntryComent = new TextArea<String>("EntryComent",new PropertyModel<String>(coment,"inputComentText"));
		Form<Void> ComentForm =new Form<Void>("ComentForm"){
			private static final long serialVersionUID = 1L;
			protected void onSubmit(){
				String userName=UserName.getInput();
				Date date=new Date(System.currentTimeMillis()+32400000);
				Text coment=new Text(EntryComent.getInput());
				ComentDto ComentDto = new ComentDto(userName,date,coment);
				ComentDto.setEntryID(EntryID);
				provider.saveComent(ComentDto);
				setResponsePage(EntryPage.class);
			}
		};
		ComentForm.add(UserName);
		ComentForm.add(EntryComent);
		return ComentForm;
	}
}
